# -*- coding: utf-8 -*-
import os

class ModuleProperty(object):
    index = 0
    def __init__(self):
        self._clazz = None
        self._Icon = ""
        self._ToolTip = ""
        self._name = ''
        self._MenuText = ""
        self._MenuItem: list = []
        self._ToolbarItem: list = []

    def appendToolbar(self, name, func):
        self._ToolbarItem.append((name, func))

    def getToolbar(self):
        return self._ToolbarItem

    def clearToolbar(self):
        self._ToolbarItem.clear()

    def appendMenu(self, name, func):
        self._MenuItem.append((name, func))

    def getMenuItem(self):
        return self._MenuItem

    def clearMenuItem(self):
        self._MenuItem.clear()

    @property
    def MenuText(self):
        return self._MenuText

    @MenuText.setter
    def MenuText(self, value):
        self._MenuText = value

    @MenuText.deleter
    def MenuText(self):
        self._MenuText = ""

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @name.deleter
    def name(self):
        self._name = None

    @property
    def clazz(self):
        return self._clazz

    @clazz.setter
    def clazz(self, value):
        self._clazz = value

    @clazz.deleter
    def clazz(self):
        self._clazz = None

    @property
    def Icon(self):
        return self._Icon

    @Icon.setter
    def Icon(self, value):
        self._Icon = value

    @Icon.deleter
    def Icon(self):
        self._Icon = ""

    @property
    def ToolTip(self):
        return self._ToolTip

    @ToolTip.setter
    def ToolTip(self, value):
        self._ToolTip = value

    @ToolTip.deleter
    def ToolTip(self):
        self._ToolTip = ""

class ModuleManager(object):
    def __init__(self, sourcepath):
        self.sourcepath = sourcepath
        self.modules: list = []
        self.index = 0
        self.loadMoules()

    def getModules(self):
        return self.modules

    def getCurModule(self):
        return self.modules[self.index]

    def getModuleHandleByName(self, name):
        for mdl in self.modules:
            if mdl.name == name:
                return mdl.clazz
        return None

    def setCurModule(self, index):
        self.index = index

    def loadMoules(self):
        modulepath = os.path.join(self.sourcepath, "module")
        for modulename in os.listdir("module"):
            print(modulename, __class__)
            path = os.path.join(modulepath, modulename)
            print(path)
            if os.path.isdir(path):
                for fn in os.listdir(path):
                    if fn == "init.py":
                        pluginName = os.path.splitext(fn)[0]
                        __import__("module." + modulename + "." + pluginName, fromlist=[pluginName])
                    elif fn == "initGui.py":
                        module = ModuleProperty()
                        module.name = modulename
                        pluginName = os.path.splitext(fn)[0]
                        print(pluginName)
                        plugin = __import__("module." + modulename + "." + pluginName, fromlist=[pluginName])
                        clazz = plugin.getModuleClass()
                        o = clazz()
                        module.clazz = o
                        module.Icon = o.Icon
                        module.ToolTip = o.ToolTip
                        module.MenuText = o.MenuText
                        self.modules.append(module)
            elif os.path.isfile(modulename):
                print("is file")
            else:
                print("non")
            # if not filename.endswith(".py") or filename.startswith("_"):
            #     continue
            # self.runPlugin(filename)


# sourcepath = os.path.dirname(__file__)
# moduleMng = ModuleManager(sourcepath)
